package com.climb.spingcloud.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.climb.spingcloud.base.PageVo;
import com.climb.spingcloud.base.R;
import com.climb.spingcloud.convert.ProductConvert;
import com.climb.spingcloud.dto.ProductDto;
import com.climb.spingcloud.entity.Product;
import com.climb.spingcloud.exception.ApplicationException;
import com.climb.spingcloud.exception.ResourceNotFoundException;
import com.climb.spingcloud.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author filteraid
 * @since 2021-07-28
 */
@RestController
@RequestMapping("/products")
public class ProductController {
    @Autowired
    private ProductService productService;

//    @ResponseStatus(HttpStatus.OK)
//    @GetMapping("/{id}")
//    public R<Product> getById(@PathVariable("id") Long id) {
//        Product product = productService.getById(id);
//        return R.success().data(product);
//    }

//@ResponseStatus(HttpStatus.OK)
//@GetMapping("/{id}")
//public R<ProductDto> getById(@PathVariable("id") Long id) {
//    Product product = productService.getById(id);
//    return R.success().data(product);
//}
    /**使用MapStruct将Product转换为ProductDto*/
    @ResponseStatus(HttpStatus.OK)
    @GetMapping("/{id}")
    public R<ProductDto> getById(@PathVariable("id") Long id) throws InterruptedException {
        Product product = productService.getById(id);

        // 传统的对象拷贝
//        ProductDto productDto = new ProductDto();
//        productDto.setId(product.getId());
//        Thread.sleep(3*1000);
        ProductDto productDto = ProductConvert.INSTANCE.entity2dto(product);

        return R.success().data(productDto);
    }
    /*查询列表*/
    @ResponseStatus(HttpStatus.OK)
    @GetMapping("")
    public R<List<ProductDto>> listAll(){
        List<Product> productList=productService.list();
        //将product转为ProductDto
        List<ProductDto> productDtoList=ProductConvert.INSTANCE.entityList2dtoList(productList);
        return R.success().data(productDtoList).message("获取商品列表成功");

    }
    /*分页*/

    @ResponseStatus(HttpStatus.OK)
    @GetMapping(value = "" ,params = {"page"})
    public R<PageVo<ProductDto>> listPage(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "2") Integer size){
        IPage<Product>  pageParam =new Page<>(page,size);
        IPage<Product> pageModel=productService.page(pageParam);
        // 将product转为ProductDto
        List<ProductDto> productDtoList = ProductConvert.INSTANCE.entityList2dtoList(pageModel.getRecords());
        PageVo<ProductDto> pageVo=new PageVo<>(page,size,(int) pageModel.getTotal(),productDtoList);
        return R.success().data(pageVo);
    }
    @ResponseStatus(HttpStatus.CREATED)
    @PostMapping("")
    public R save(@RequestBody ProductDto productDto) {
        // 将ProductDto转为Product
        Product product = ProductConvert.INSTANCE.dto2entity(productDto);

        boolean result = productService.save(product);
        if (result) {
            return R.success().message("保存成功");
        } else {
            throw new ApplicationException("保存失败");
        }
    }

    @ResponseStatus(HttpStatus.CREATED)
    @PutMapping("/{id}")
    public R updateById(@PathVariable Long id, @RequestBody ProductDto productDto) {
        productDto.setId(id);
        // 将ProductDto转为Product
        Product product = ProductConvert.INSTANCE.dto2entity(productDto);

        boolean result = productService.updateById(product);
        if (result) {
            return R.success().message("修改成功");
        } else {
            throw new ResourceNotFoundException("数据不存在");
        }
    }

    // 逻辑数据删除，初始值在数据库中用默认值表示
    @ResponseStatus(HttpStatus.OK)
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable Long id) {
        // 将ProductDto转为Product
        boolean result = productService.removeById(id);

        if (result) {
            return R.success().message("删除成功");
        } else {
            throw new ResourceNotFoundException("数据不存在");
        }
    }
}

